import AAA.mapper.StudentMapper;
import AAA.pojo.Student;
import AAA.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.testng.annotations.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ttttttest {

    @Test
    public void testSelectById() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectById(2L);
        for (Student student : students) {
            System.out.println(student);
        }
        sqlSession.close();

    }

    @Test
    public void testSelectByName() {
        StudentMapper mapper = SqlSessionUtil.openSession().getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByName("张三");
        students.forEach(student -> System.out.println(student));
        SqlSessionUtil.openSession().close();
    }

    @Test
    public void testSelectBirth() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = format.parse("2024-08-06");

        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByBirth(parse);
        students.forEach(student -> System.out.println(student));
    }

    @Test
    public void testSelectBySex() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        List<Student> students = mapper.selectBySex('女');
//        Character nan = Character.valueOf('男');
        List<Student> students = mapper.selectBySex("女");
        students.forEach(student -> System.out.println(student));
        sqlSession.close();
    }

    @Test
    public void testInsertByMap() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map<String, Object> map = new HashMap<>();

        map.put("name", "lisi");
        map.put("sex", "男");
        map.put("birth", new Date());
        map.put("height", 1.7);
        map.put("age", 20);


        mapper.insertStudentByMap(map);

        sqlSession.commit();
        sqlSession.close();


    }

    @Test
    public void testInsertByPOJO() {

        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        Student student = new Student(null,"张三",200,1.7,new Date(),"男");
        Student student = new Student();
        student.setAge(2000);
        student.setBirth(new Date());
        student.setHeight(1.77777);
        student.setName("张三san");
        student.setSex("男");
        mapper.insertStudentByPOJO(student);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testnameAndSex() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByNameAndSex("李四", "女");
        students.forEach(student -> System.out.println(student));
        sqlSession.close();

    }


    @Test
    public void testnameAndSexx(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByNameAndSexx("lisi", "男");
        students.forEach(student-> System.out.println(student));
        sqlSession.close();

    }


}
